define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/quality_inventory/index' + location.search,
                    detail_url: 'scanwork/quality_inventory/detail',
                    table: 'scanwork_quality_inventory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'allocation.plan.plan_code', title: '小工单计划编码', operate: 'LIKE'},
                        {field: 'allocation.order.order_name', title: '订单名称', operate: 'LIKE'},
                        {field: 'productModel.name', title: '产品型号', operate: 'LIKE'},
                        {field: 'process.name', title: '工序', operate: 'LIKE'},
                        {field: 'quantity', title: '质检数量'},
                        {field: 'qualified_quantity', title: '合格数量', formatter: function(value, row, index) {
                            return '<span class="text-success"><strong>' + value + '</strong></span>';
                        }},
                        {field: 'unqualified_quantity', title: '不合格数量', formatter: function(value, row, index) {
                            if (value > 0) {
                                return '<span class="text-danger"><strong>' + value + '</strong></span>';
                            }
                            return value;
                        }},
                        {field: 'quality_result', title: '质检结果', operate: 'FIND_IN_SET', searchList: {1: '合格', 2: '不合格'}, formatter: function(value, row, index) {
                            if (value == 1) {
                                return '<span class="label label-success">合格</span>';
                            } else {
                                return '<span class="label label-danger">不合格</span>';
                            }
                        }},
                        {field: 'handle_method', title: '处理方式', operate: 'FIND_IN_SET', searchList: {1: '合格', 2: '返工', 3: '让步接收', 4: '报废'}, formatter: function(value, row, index) {
                            var map = {1: 'success', 2: 'warning', 3: 'info', 4: 'danger'};
                            var text = {1: '合格', 2: '返工', 3: '让步接收', 4: '报废'};
                            return '<span class="label label-' + (map[value] || 'default') + '">' + (text[value] || '--') + '</span>';
                        }},
                        {field: 'inventory_status', title: '入库状态', operate: 'FIND_IN_SET', searchList: {0: '待入库', 1: '已入库'}, formatter: function(value, row, index) {
                            if (value == 1) {
                                return '<span class="label label-success">已入库</span>';
                            } else {
                                return '<span class="label label-warning">待入库</span>';
                            }
                        }},
                        {field: 'user.nickname', title: '报工员', operate: 'LIKE'},
                        {field: 'qualityUser.nickname', title: '质检员', operate: 'LIKE'},
                        {field: 'inventoryUser.nickname', title: '入库操作员', operate: 'LIKE'},
                        {field: 'check_time', title: '质检时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'inventory_time', title: '入库时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        pending: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/quality_inventory/pending' + location.search,
                    execute_url: 'scanwork/quality_inventory/execute',
                    table: 'scanwork_quality_inventory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'allocation.plan.plan_code', title: '小工单计划编码', operate: 'LIKE'},
                        {field: 'allocation.order.order_name', title: '订单名称', operate: 'LIKE'},
                        {field: 'productModel.name', title: '产品型号', operate: 'LIKE'},
                        {field: 'process.name', title: '工序', operate: 'LIKE'},
                        {field: 'quantity', title: '质检数量'},
                        {field: 'qualified_quantity', title: '合格数量', formatter: function(value, row, index) {
                            return '<span class="text-success"><strong>' + value + '</strong></span>';
                        }},
                        {field: 'unqualified_quantity', title: '不合格数量', formatter: function(value, row, index) {
                            if (value > 0) {
                                return '<span class="text-danger"><strong>' + value + '</strong></span>';
                            }
                            return value;
                        }},
                        {field: 'quality_result', title: '质检结果', operate: 'FIND_IN_SET', searchList: {1: '合格', 2: '不合格'}, formatter: function(value, row, index) {
                            if (value == 1) {
                                return '<span class="label label-success">合格</span>';
                            } else {
                                return '<span class="label label-danger">不合格</span>';
                            }
                        }},
                        {field: 'handle_method', title: '处理方式', operate: 'FIND_IN_SET', searchList: {1: '合格', 2: '返工', 3: '让步接收', 4: '报废'}, formatter: function(value, row, index) {
                            var map = {1: 'success', 2: 'warning', 3: 'info', 4: 'danger'};
                            var text = {1: '合格', 2: '返工', 3: '让步接收', 4: '报废'};
                            return '<span class="label label-' + (map[value] || 'default') + '">' + (text[value] || '--') + '</span>';
                        }},
                        {field: 'user.nickname', title: '报工员', operate: 'LIKE'},
                        {field: 'qualityUser.nickname', title: '质检员', operate: 'LIKE'},
                        {field: 'check_time', title: '质检时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var buttons = [];
                            buttons.push('<a class="btn btn-success btn-xs btn-execute" href="' + Fast.api.fixurl('scanwork/quality_inventory/execute?id=' + row.id) + '" title="执行入库"><i class="fa fa-arrow-down"></i> 入库</a>');
                            return buttons.join(' ');
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        execute: function () {
            Controller.api.bindevent();
            Controller.api.initExecuteForm();
        },
        detail: function () {
            // 详情页无需特殊处理
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            initExecuteForm: function() {
                // 表单提交
                $('#execute-form').on('submit', function(e) {
                    e.preventDefault();
                    
                    var formData = {
                        'row[inventory_quantity]': $('#c-inventory_quantity').val(),
                        'row[storage_id]': $('#c-storage_id').val(),
                        'row[remark]': $('#c-remark').val()
                    };
                    
                    $.post('', formData, function(data) {
                        if (data.code == 1) {
                            Toastr.success(data.msg);
                            setTimeout(function() {
                                window.location.href = Fast.api.fixurl('scanwork/quality_inventory/pending');
                            }, 1500);
                        } else {
                            Toastr.error(data.msg);
                        }
                    });
                });
            }
        }
    };
    return Controller;
});
